import {Component, OnDestroy, OnInit, TemplateRef, ViewChild, ViewEncapsulation} from '@angular/core';

import {FormGroup} from '@angular/forms';
import {MatDialog, MatDialogRef} from '@angular/material/dialog';

import {baseAnimations} from '@base/animations';
import {BaseConfirmDialogComponent} from '@base/components/confirm-dialog/confirm-dialog.component';

import {Subject} from 'rxjs';
import {takeUntil} from 'rxjs/internal/operators';

import {SettingRolesService} from './roles.service';
import {RolesFormDialogComponent} from './roles-form/roles-form.component';

@Component({
    selector: 'permissions-roles',
    templateUrl: './roles.component.html',
    styleUrls: ['./roles.component.scss'],
    animations: baseAnimations,
    encapsulation: ViewEncapsulation.None
})


export class RolesComponent implements OnInit, OnDestroy {
    @ViewChild('dialogContent', {static: false})
    dialogContent: TemplateRef<any>;
    dialogRef: any;
    confirmDialogRef: MatDialogRef<BaseConfirmDialogComponent>;
    roles: any;
    public data: object[] = [];
    // Private
    private _unsubscribeAll: Subject<any>;

    /**
     * Constructor
     */
    constructor(
        private _settingRolesService: SettingRolesService,
        private _matDialog: MatDialog
    ) {
        // Set the private defaults
        this._unsubscribeAll = new Subject();




        this._settingRolesService.onRolesChanged
            .pipe(takeUntil(this._unsubscribeAll))
            .subscribe(roles => {
                this.roles = roles;
            });

        console.log(this.roles);

    }



    ngOnInit(): void {

    }

    /**
     * On destroy
     */
    ngOnDestroy(): void {
        // Unsubscribe from all subscriptions
        this._unsubscribeAll.next();
        this._unsubscribeAll.complete();
    }

    // -----------------------------------------------------------------------------------------------------
    // @ Public methods
    // -----------------------------------------------------------------------------------------------------

    /**
     * New Permit
     */
    newRole(): void {
        this.dialogRef = this._matDialog.open(RolesFormDialogComponent, {
            panelClass: 'roles-form-dialog',
            data: {
                action: 'new'
            }
        });

        this.dialogRef.afterClosed()
            .subscribe((response: FormGroup) => {
                if (!response) {
                    return;
                }

                this._settingRolesService.saveRole(response.getRawValue());
            });
    }

    /**
     * Edit Permit
     *
     * @param role
     */
    editRole(role): void {
        console.log(role);
        this.dialogRef = this._matDialog.open(RolesFormDialogComponent, {
            panelClass: 'roles-form-dialog',
            data: {
                role: role,
                action: 'edit'
            }
        });

        this.dialogRef.afterClosed()
            .subscribe(response => {
                if (!response) {
                    return;
                }
                const actionType: string = response[0];
                const formData: FormGroup = response[1];
                switch (actionType) {
                    /**
                     * Save
                     */
                    case 'save':

                        this._settingRolesService.saveRole(formData.getRawValue());

                        break;
                    /**
                     * Delete
                     */
                    case 'delete':

                        this.deleteRole(role);

                        break;
                }
            });
    }

    /**
     * Delete roles
     */
    deleteRole(role): void {
        this.confirmDialogRef = this._matDialog.open(BaseConfirmDialogComponent, {
            disableClose: false
        });

        this.confirmDialogRef.componentInstance.confirmMessage = 'Are you sure you want to delete?';

        this.confirmDialogRef.afterClosed().subscribe(result => {
            if (result) {
                this._settingRolesService.deleteRole(role);
            }
            this.confirmDialogRef = null;
        });

    }

}
